#include <stdio.h>
#include "uemclient.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "cJSON.h"
#include <time.h>
#define PORT 6868
#define IP "192.168.6.128"
/*
登录菜单函数实现；
*/
void user_login_register()
{
    printf("******************登录注册菜单**********************\n");
    printf("********************1.login************************\n");
    printf("********************2.register*********************\n");
    printf("********************3.exit*************************\n");
    printf("***************************************************\n");
}

void login()
{
    int sockfd = 0;
    int ret = 0;
    char buf[100] = {0};
    int num = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct login_register user;
    memset(&user,0,sizeof(user));
    
    while(num != 3)
    {
        memset(&user,0,sizeof(user));
        user.identity = 1;
        strcpy(user.type,"login");
        printf("请输入你的账号:\n");
        scanf("%d",&user.id);
        printf("请输入你的密码:\n");
        scanf("%s",user.pwd);
        
        json = cJSON_CreateObject();
        cJSON_AddItemToObject(json,"identity",cJSON_CreateNumber(user.identity));
        cJSON_AddItemToObject(json,"type",cJSON_CreateString(user.type));
        cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(user.id));
        cJSON_AddItemToObject(json,"pwd",cJSON_CreateString(user.pwd));

        dest = cJSON_Print(json);

        ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
        if(ret == -1)
        {
            perror("sendto");
            exit(-1);
        }

        free(dest);
        cJSON_Delete(json);

        memset(buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        printf("%s\n",buf);

        if(strcmp(buf,"success") == 0)
        {
            break;
        }

        num = num + 1;

    }

    user_implement();
    close(sockfd);
}

void _register()
{
    int sockfd = 0;
    int ret = 0;
    char buf[100] = {0};

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct login_register user;
    memset(&user,0,sizeof(user));
    while(1)
    {
        memset(&user,0,sizeof(user));
        user.identity = 1;
        strcpy(user.type,"register");
        printf("请输入你的账号:\n");
        scanf("%d",&user.id);
        printf("请输入你的密码:\n");
        scanf("%s",user.pwd);
        printf("请输入你的姓名:\n");
        scanf("%s",user.name);
        printf("请输入的电话号码:\n");
        scanf("%s",user.phoneNumber);
        
        json = cJSON_CreateObject();
        cJSON_AddItemToObject(json,"identity",cJSON_CreateNumber(user.identity));
        cJSON_AddItemToObject(json,"type",cJSON_CreateString(user.type));
        cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(user.id));
        cJSON_AddItemToObject(json,"pwd",cJSON_CreateString(user.pwd));
        cJSON_AddItemToObject(json,"name",cJSON_CreateString(user.name));
        cJSON_AddItemToObject(json,"phoneNumber",cJSON_CreateString(user.phoneNumber));

        dest = cJSON_Print(json);

        ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
        if(ret == -1)
        {
            perror("sendto");
            exit(-1);
        }
        
        free(dest);
        cJSON_Delete(json);

        memset(buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        printf("%s\n",buf);

        if(strcmp(buf,"success") == 0)
        {
            break;
        }
    }
    close(sockfd);    

}

void choose()
{
    int num = 0;
    while(1)
    {
        user_login_register();
        printf("请输入你的选择\n");
        scanf("%d",&num);
        switch(num)
        {
            case 1:login();
            break;
            case 2:_register();
            break;
            case 3:
            break;
            default:
            printf("你输入错误，请重新输入\n");
            break;
        }
        if(num == 3)
        {
            break;
        }
    }
}

/*
用户菜单
*/
void user_meun()
{
    printf("********************************************************\n");
    printf("********************用户功能菜单*************************\n");
    printf("**********************1.绑定设备*************************\n");
    printf("**********************2.  充值  *************************\n");
    printf("**********************3.查看个人信息*********************\n");
    printf("**********************4.查看充值记录*********************\n");
    printf("**********************5.查看远程控制记录*****************\n");
    printf("**********************6.解除绑定设备*********************\n");
    printf("**********************7.退出****************************\n");
    printf("********************************************************\n");
}

/*绑定设备函数*/

void bind_equip()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct bindEquit ube;
    memset(&ube,0,sizeof(ube));

    strcpy(ube.type,"bind");
    printf("请输入设备类型:\n");
    scanf("%d",&ube.equtype);
    printf("请输入你的id:\n");
    scanf("%d",&ube.uid);
    printf("请输入设备id:\n");
    scanf("%d",&ube.eid);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(ube.type));
    cJSON_AddItemToObject(json,"equtype",cJSON_CreateNumber(ube.equtype));
    cJSON_AddItemToObject(json,"uid",cJSON_CreateNumber(ube.uid));
    cJSON_AddItemToObject(json,"eid",cJSON_CreateNumber(ube.eid));

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    char buf[100] = {0};

    ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
    if(ret == -1)
    {
        perror("recvfrom");
        exit(-1);
    }

    printf("%s\n",buf);

    close(sockfd); 

}
//充值
void user_top_up()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct top_up utu;
    memset(&utu,0,sizeof(utu));

    strcpy(utu.type,"recharge");
    printf("请输入设备类型:\n");
    scanf("%d",&utu.equtype);
    printf("请输入你的id:\n");
    scanf("%d",&utu.id);
    printf("请输入你的金额:\n");
    scanf("%d",&utu.money);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(utu.type));
    cJSON_AddItemToObject(json,"equtype",cJSON_CreateNumber(utu.equtype));
    cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(utu.id));
    cJSON_AddItemToObject(json,"money",cJSON_CreateNumber(utu.money));

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    char buf[100] = {0};

    ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
    if(ret == -1)
    {
        perror("recvfrom");
        exit(-1);
    }
    
    printf("%s\n",buf);

    close(sockfd);

}

//查看个人信息
void view_userinfo()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct check_infor check_per_info;
    memset(&check_per_info,0,sizeof(check_per_info));

    strcpy(check_per_info.type,"getuser");
    printf("请输入你的id:\n");
    scanf("%d",&check_per_info.id);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(check_per_info.type));
    cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(check_per_info.id));

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    cJSON *json1 = NULL;
    struct userInfo recep;
    memset(&recep,0,sizeof(recep));
    char buf[1024] = {0};
    cJSON *node = NULL;

    ret = recvfrom(sockfd,buf,1023,0,NULL,NULL);
    if(ret == -1)
    {
        perror("recvfrom");
        exit(-1);
    }

    json1 = cJSON_Parse(buf);

    node = cJSON_GetObjectItem(json1,"id");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        recep.id = node->valueint;
    }

    node = cJSON_GetObjectItem(json1,"name");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(recep.name,node->valuestring);
    }

    node = cJSON_GetObjectItem(json1,"waterid");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        recep.waterid = node->valueint;
    }

    node = cJSON_GetObjectItem(json1,"elecid");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        recep.elecid = node->valueint;
    }

    node = cJSON_GetObjectItem(json1,"waterbalance");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        recep.waterbalance = node->valueint;
    }

    node = cJSON_GetObjectItem(json1,"elecbalance");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        recep.elecbalance = node->valueint;
    }

    node = cJSON_GetObjectItem(json1,"phoneNumber");
    if(node == NULL)
    {
        printf("can't find");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(recep.phoneNumber,node->valuestring);
    }

    printf("id:%d\n",recep.id);
    printf("name:%s\n",recep.name);
    printf("waterid:%d\n",recep.waterid);
    printf("elecid:%d\n",recep.elecid);
    printf("waterbalance:%d\n",recep.waterbalance);
    printf("elecbalance:%d\n",recep.elecbalance);
    printf("phoneNumber:%s\n",recep.phoneNumber);

    cJSON_Delete(json);

    close(sockfd);

}

//查看充值记录
void view_top_up_record()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct check_infor check_per_info;
    memset(&check_per_info,0,sizeof(check_per_info));

    strcpy(check_per_info.type,"getrech");
    printf("请输入你的id:\n");
    scanf("%d",&check_per_info.id);
    

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(check_per_info.type));
    cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(check_per_info.id)); 

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    cJSON *json1 = NULL;
    struct rechrecord record;
    memset(&record,0,sizeof(record));
    char buf[1024] = {0};
    cJSON *node = NULL;
    while(1)
    {
        memset(&record,0,sizeof(record));
        memset(buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,1023,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        json1 = cJSON_Parse(buf);

        node = cJSON_GetObjectItem(json1,"id");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            record.id = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"name");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(record.name,node->valuestring);
        }

        node = cJSON_GetObjectItem(json1,"fee");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            record.fee = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"rechType");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(record.rechType,node->valuestring);
        }

        node = cJSON_GetObjectItem(json1,"rechTime");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(record.rechTime,node->valuestring);
        }

        printf("id:%d\n",record.id);
        printf("name:%s\n",record.name);
        printf("fee:%d\n",record.fee);
        printf("rechType:%s\n",record.rechType);
        printf("rechTime:%s\n",record.rechTime);
    }
    cJSON_Delete(json);

    close(sockfd);


}

//查看控制记录
void view_ctl_record()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct check_infor check_per_info;
    memset(&check_per_info,0,sizeof(check_per_info));

    strcpy(check_per_info.type,"getoper");
    printf("请输入你的id:\n");
    scanf("%d",&check_per_info.id);
    

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(check_per_info.type));
    cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(check_per_info.id));

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    cJSON *json1 = NULL;
    struct operrecord ctlcord;
    memset(&ctlcord,0,sizeof(ctlcord));
    char buf[1024] = {0};
    cJSON *node = NULL;
    while(1)
    {
        memset(&ctlcord,0,sizeof(ctlcord));
        memset(buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,1023,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        json1 = cJSON_Parse(buf);

        node = cJSON_GetObjectItem(json1,"operation");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(ctlcord.operation,node->valuestring);
        }

        node = cJSON_GetObjectItem(json1,"reson");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(ctlcord.reson,node->valuestring);
        }

        node = cJSON_GetObjectItem(json1,"equid");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            ctlcord.equid = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"euqType");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(ctlcord.euqType,node->valuestring);
        }

        node = cJSON_GetObjectItem(json1,"operTime");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(ctlcord.operTime,node->valuestring);
        }

        printf("operation:%s\n",ctlcord.operation);
        printf("reson:%s\n",ctlcord.reson);
        printf("equid:%d\n",ctlcord.equid);
        printf("euqType:%s\n",ctlcord.euqType);
        printf("operTime:%s\n",ctlcord.operTime);
    }
    cJSON_Delete(json);

    close(sockfd);

}

/*解除绑定设备函数*/

void unbind_equip()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct bindEquit ube;
    memset(&ube,0,sizeof(ube));

    strcpy(ube.type,"unbind");
    printf("请输入设备类型:\n");
    scanf("%d",&ube.equtype);
    printf("请输入你的id:\n");
    scanf("%d",&ube.uid);
    printf("请输入设备id:\n");
    scanf("%d",&ube.eid);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(ube.type));
    cJSON_AddItemToObject(json,"equtype",cJSON_CreateNumber(ube.equtype));
    cJSON_AddItemToObject(json,"uid",cJSON_CreateNumber(ube.uid));
    cJSON_AddItemToObject(json,"eid",cJSON_CreateNumber(ube.eid));

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    char buf[100] = {0};

    ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
    if(ret == -1)
    {
        perror("recvfrom");
        exit(-1);
    }

    printf("%s\n",buf);

    close(sockfd); 

}

//实现功能函数
void user_implement()
{
    int num = 0;
    while(1)
    {
        user_meun();
        printf("请输入你的选择:\n");
        scanf("%d",&num);

        switch(num)
        {
            case 1:
            bind_equip();
            break;
            case 2:
            user_top_up();
            break;
            case 3:
            view_userinfo();
            break;
            case 4:
            view_top_up_record();
            break;
            case 5:
            view_ctl_record();
            break;
            case 6:
            unbind_equip();
            break;
            case 7:
            break;
            default:
            printf("你的输入错误:\n");
            break;
        }
        if(num == 7)
        {
            return;
        }
    }

}

/*管理员注册登录*/
void manager_login()
{
    int sockfd = 0;
    int ret = 0;
    char buf[100] = {0};
    int num = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct login_register user;
    memset(&user,0,sizeof(user));
    while(num != 3)
    {
        memset(&user,0,sizeof(user));
        user.identity = 2;
        strcpy(user.type,"login");
        printf("请输入你的账号:\n");
        scanf("%d",&user.id);
        printf("请输入你的密码:\n");
        scanf("%s",user.pwd);
        
        json = cJSON_CreateObject();
        cJSON_AddItemToObject(json,"identity",cJSON_CreateNumber(user.identity));
        cJSON_AddItemToObject(json,"type",cJSON_CreateString(user.type));
        cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(user.id));
        cJSON_AddItemToObject(json,"pwd",cJSON_CreateString(user.pwd));

        dest = cJSON_Print(json);

        ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
        if(ret == -1)
        {
            perror("sendto");
            exit(-1);
        }

        free(dest);
        cJSON_Delete(json);

        memset(buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        printf("%s\n",buf);

        if(strcmp(buf,"success") == 0)
        {
            break;
        }
            
        num = num + 1;
    }

    manger_fun(); 
    close(sockfd);    

}

void _manager_register()
{
    int sockfd = 0;
    int ret = 0;
    char buf[100] = {0};

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct login_register user;
    memset(&user,0,sizeof(user));
    while(1)
    {
        memset(&user,0,sizeof(user));
        user.identity = 2;
        strcpy(user.type,"register");
        printf("请输入你的账号:\n");
        scanf("%d",&user.id);
        printf("请输入你的密码:\n");
        scanf("%s",user.pwd);
        printf("请输入你的姓名:\n");
        scanf("%s",user.name);
        printf("请输入的电话号码:\n");
        scanf("%s",user.phoneNumber);
        
        json = cJSON_CreateObject();
        cJSON_AddItemToObject(json,"identity",cJSON_CreateNumber(user.identity));
        cJSON_AddItemToObject(json,"type",cJSON_CreateString(user.type));
        cJSON_AddItemToObject(json,"id",cJSON_CreateNumber(user.id));
        cJSON_AddItemToObject(json,"pwd",cJSON_CreateString(user.pwd));
        cJSON_AddItemToObject(json,"name",cJSON_CreateString(user.name));
        cJSON_AddItemToObject(json,"phoneNumber",cJSON_CreateString(user.phoneNumber));

        dest = cJSON_Print(json);

        ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
        if(ret == -1)
        {
            perror("sendto");
            exit(-1);
        }
       
        free(dest);
        cJSON_Delete(json);

        memset(buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,99,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        printf("%s\n",buf);

        if(strcmp(buf,"success") == 0)
        {
            break;
        }
    }
    close(sockfd);    

}

/*
管理员选择菜单
*/
void manger_menu()
{
    int num = 0;
    while(1)
    {
        printf("**********************************************\n");
        printf("******************1.登录**********************\n");
        printf("******************2.注册**********************\n");
        printf("******************3.退出**********************\n");
        printf("**********************************************\n");

        printf("请输入你的选择\n");
        scanf("%d",&num);

        switch(num)
        {
            case 1:
            manager_login();
            break;
            case 2:
            _manager_register();
            break;
            case 3:break;
            default:
            printf("你的输入错误:\n");
            break;
        }
        if(num == 3)
        {
            break;
        }
    }
    
}

/*
管理员功能
*/
void manger_fun()
{
    int num = 0;
    while(1)
    {
        printf("**************************************\n");
        printf("**************1.查看用户信息***********\n");
        printf("**************2.通过id查寻用户信息*****\n");
        printf("**************3.退出******************\n");
        printf("**************************************\n");

        printf("请输入你的选择:\n");
        scanf("%d",&num);

        switch(num)
        {
            case 1:
            view_user_all_info();
            break;
            case 2:
            view_userinfo();
            break;
            case 3:
            break;
            default:
            printf("你的输入错误:\n");
            break;
        }

        if(num == 4)
        {
            break;
        }
    }
}

/*
设备采集数据;
*/
void collect_data()
{
    int sockfd = 0;
    int ret = 0;
    srand(time(NULL));
    int watertemp = 0;
    int electemp = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct equipment equip_data;
    memset(&equip_data,0,sizeof(equip_data));

    strcpy(equip_data.type,"gather");
    printf("请输入你的水表id:\n");
    scanf("%d",&equip_data.waterid);
    equip_data.waterbalance = 300;
    printf("请输入你的电表id:\n");
    scanf("%d",&equip_data.elecid);
    equip_data.elecbalance = 300;

    watertemp = equip_data.waterid;
    electemp = equip_data.elecid;

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(equip_data.type));
    cJSON_AddItemToObject(json,"waterid",cJSON_CreateNumber(equip_data.waterid));
    cJSON_AddItemToObject(json,"waterbalance",cJSON_CreateNumber(equip_data.waterbalance));
    cJSON_AddItemToObject(json,"elecid",cJSON_CreateNumber(equip_data.elecid));
    cJSON_AddItemToObject(json,"elecbalance",cJSON_CreateNumber(equip_data.elecbalance));

    dest = cJSON_Print(json);
    while(1)
    {
        ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
        if(ret == -1)
        {
            perror("sendto");
            exit(-1);
        }

        strcpy(equip_data.type,"gather");
        equip_data.waterid = watertemp;
        equip_data.waterbalance = equip_data.waterbalance -((rand()%5) + 1);
        if(equip_data.waterbalance <= 0)
        {
            break;
        }

        equip_data.elecid = electemp;
        equip_data.elecbalance = equip_data.elecbalance -((rand()%5) + 1);
        if(equip_data.elecbalance <= 0)
        {
            break;
        }

        json = cJSON_CreateObject();
        cJSON_AddItemToObject(json,"type",cJSON_CreateString(equip_data.type));
        cJSON_AddItemToObject(json,"waterid",cJSON_CreateNumber(equip_data.waterid));
        cJSON_AddItemToObject(json,"waterbalance",cJSON_CreateNumber(equip_data.waterbalance));
        cJSON_AddItemToObject(json,"elecid",cJSON_CreateNumber(equip_data.elecid));
        cJSON_AddItemToObject(json,"elecbalance",cJSON_CreateNumber(equip_data.elecbalance));

        dest = cJSON_Print(json);

        sleep(5);

    }

    free(dest);
    cJSON_Delete(json);

    struct waterorelec_fee wef;
    memset(&wef,0,sizeof(wef));
  
    ret = recvfrom(sockfd,&wef,sizeof(wef),0,NULL,NULL);
    if(ret == -1)
    {
        perror("recvfrom");
        exit(-1);
    }

    printf("%d",wef.equtype);
    printf("%d",wef.fee);

    if(wef.equtype == 1)
    {
        equip_data.waterbalance += wef.fee;
    }else if(wef.equtype == 2)
    {
        equip_data.elecbalance += wef.fee;
    }

}

/*
管理员的查看用户信息
*/
void view_user_all_info()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    cJSON* json = NULL;
    char* dest = NULL;

    struct check_infor check_per_info;
    memset(&check_per_info,0,sizeof(check_per_info));

    strcpy(check_per_info.type,"getuser");

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateString(check_per_info.type));

    dest = cJSON_Print(json);

    ret = sendto(sockfd,dest,strlen(dest),0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
    if(ret == -1)
    {
        perror("sendto");
        exit(-1);
    }

    free(dest);
    cJSON_Delete(json);

    cJSON *json1 = NULL;
    struct userInfo recep;
    memset(&recep,0,sizeof(recep));
    char buf[1024] = {0};
    cJSON *node = NULL;
    while(1)
    {
        memset(&recep,0,sizeof(recep));
        memset(&buf,0,sizeof(buf));
        ret = recvfrom(sockfd,buf,1023,0,NULL,NULL);
        if(ret == -1)
        {
            perror("recvfrom");
            exit(-1);
        }

        json1 = cJSON_Parse(buf);

        node = cJSON_GetObjectItem(json1,"id");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            recep.id = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"name");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(recep.name,node->valuestring);
        }

        node = cJSON_GetObjectItem(json1,"waterid");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            recep.waterid = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"elecid");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            recep.elecid = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"waterbalance");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            recep.waterbalance = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"elecbalance");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            recep.elecbalance = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"phonenumber");
        if(node == NULL)
        {
            printf("can't find");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(recep.phoneNumber,node->valuestring);
        }

        printf("id:%d\n",recep.id);
        printf("name:%s\n",recep.name);
        printf("waterid:%d\n",recep.waterid);
        printf("elecid:%d\n",recep.elecid);
        printf("waterbalance:%d\n",recep.waterbalance);
        printf("elecbalance:%d\n",recep.elecbalance);
        printf("phoneNumber:%s\n",recep.phoneNumber);
    }
    
    cJSON_Delete(json);

    close(sockfd);
}

int create_socket()
{
    int sockfd = 0;
    int ret = 0;

    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    sockfd = socket(AF_INET,SOCK_DGRAM,0);
    if(sockfd == -1)
    {
        perror("socket");
        exit(-1);
    }

    return sockfd;
}
